package jdbc;

import entity.Customer;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JDBCUtils {

    public static JDBCUtils getJDBCUtils() {
        return new JDBCUtils();
    }


    public int updateCustomer(String sql, Object... args) {
        PreparedStatement ps = null;
        Connection conn = null;
        try {
            conn = JDBCUtility.getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            int line = ps.executeUpdate();
            return line;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public Customer getCustomer(String sql, int id) {
        Customer customer = null;
        try {
            Connection conn = JDBCUtility.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setObject(1, id);
            ResultSet rs = ps.executeQuery();
            customer = null;
            while (rs.next()) {
                customer = new Customer(rs.getInt("id"), rs.getString("name")
                        , rs.getString("gender"), rs.getInt("age"), rs.getString("phone"),
                        rs.getString("email"));
            }
            return customer;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public <T> List<T> getList(Class<T> clazz, String sql, Object... args) {
        try {
            Connection conn = JDBCUtility.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            ResultSet rs = ps.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            ArrayList<T> list = new ArrayList<>();
            while (rs.next()) {
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    Object columnValue = rs.getObject(i + 1);
                    String columnLabel = metaData.getColumnLabel(i + 1);
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t, columnValue);
                }
                list.add(t);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Customer> getListAnyCustomer(String sql , int startIndex, int length )  {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
             conn = JDBCUtility.getConnection();
            Customer customer = null;
             ps = conn.prepareStatement(sql);

            ps.setInt(1,(startIndex-1) * length );
            ps.setInt(2,length);

            rs = ps.executeQuery();
            ArrayList<Customer> list = new ArrayList<>();
            while (rs.next()){
                customer = new Customer(rs.getInt("id"),rs.getString("name"),rs.getString("gender")
                ,rs.getInt("age"),rs.getString("phone"),rs.getString("email"));

                list.add(customer);
            }

            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
